const path = require('path');
var HtmlWebpackPlugin = require('html-webpack-plugin'); //打包html的插件
const ExtractTextPlugin = require('extract-text-webpack-plugin');

module.exports = {
  entry: './src/index.js',
  output: {
    filename: 'js/[name]-[hash:6].js',
    path: path.resolve(__dirname, 'dist')
  },
  module: {
    rules: [{
        test: /\.css$/,
        use: ExtractTextPlugin.extract({
          fallback: "style-loader",
          use: "css-loader"
        }),
      }, {
        test: /\.scss$/,
        use: [{
          loader: "style-loader"
        }, {
          loader: "css-loader"
        }, {
          loader: "sass-loader",

        }]
      }, {
        test: /\.(png|jpg|gif|svg|bmp|eot|woff|woff2|ttf)$/,
        use: {
          loader: 'url-loader',
          options: {
            limit: 5 * 1024, // 图片大小 > limit 使用file-loader, 反之使用url-loader
            outputPath: 'imgs/' // 指定打包后的图片位置
          }
        }
      }

    ]
  },
  optimization: {
    splitChunks: {
      cacheGroups: {
        vendor: {
          chunks: 'initial',
          minChunks: 1,
          maxInitialRequests: 5,
          minSize: 0,
          name: 'vendor'
        }
      }
    },
    runtimeChunk: {
      name: 'manifest'
    }
  },
  plugins: [
    // new ExtractTextPlugin('style.css')
    //if you want to pass in options, you can do so:
    // new ExtractTextPlugin('style.css'),
    new ExtractTextPlugin({
      filename: 'css/[name].css',

    }),
    new HtmlWebpackPlugin({
      favicon: 'favicon.ico',
      filename: 'index.html',
      template: 'index.html',
      minify: {
        removeAttributeQuotes: false // 移除属性的引号
      }
    })
  ]
};